

clear
set more 1
set type double


/*this file estimates pass-through regressions using monthly data for 2012-2020m11
*for different horizon h
*no market share variables in this (dropped because they were insignificant)
*******************************************************************************

*0. combine export data
****************************************
use  ../../rawdata/X_2020, clear	
	append using ../../rawdata/X_2019
	append using ../../rawdata/X_2018
	append using ../../rawdata/X_2017
	append using ../../rawdata/X_2016
	append using ../../rawdata/X_2015
	append using ../../rawdata/X_2014
	append using ../../rawdata/X_2013
	append using ../../rawdata/X_2012

	drop if flow=="XI"
	replace weight=. if weight==0
	replace units=. if units==0
	drop if missing(units) & missing(weight)
	collapse (sum) value weight units, by(vat month year cncode land)
	rename land cty_code
	gen date  = ym(year, month)
	format date %tm
		
	gen uv1=value/weight
	gen uv2=value/units 
	
	rename weight q1
	rename units q2
	
	egen fpd = group(vat cncode cty_code)
save temp/exportdata, replace


*local lag = 12

forv lag=1/24 {

*1. prepare exchange rate data
*this was put together by Noah in data/exchange_rates
**************************************************************
use ../rawdata/xr_monthly_2020, clear
	gen date  = ym(year, month)
	format date %tm
	keep date cty_name cty_code xr_m
	duplicates drop
	drop if missing(xr_m)
	egen cty_num=group(cty_code)
	tsset cty_num date

*xr_m is euro per destination
*redefine exchange rate so an increase is a depreciation of the destination currency
*gen destination per euro
	gen xE=1/xr_m
	gen dxE=ln(xE/l`lag'.xE)
	
*drop outliers: Venezuela, Sudan, Uzbekastan 
	drop if cty_code=="VE"|cty_code=="SD"|cty_code=="UZ"
	drop if abs(dxE)>1
	keep cty_code date xE dxE
save temp/er_monthly_xE,replace


use ../rawdata/xr_monthly_2020, clear
	gen date  = ym(year, month)
	format date %tm
	keep date cty_name cty_code xr_m
	duplicates drop
	drop if missing(xr_m)
	egen cty_num=group(cty_code)
	

preserve
	keep if cty_code=="US"
	gen euro_dollar=xr_m 
	gen xED=1/xr_m
	keep date euro_dollar xED
	duplicates drop
	save temp/euro_dollar, replace
restore
		
	merge m:1 date using temp/euro_dollar, keep(match) nogen
	tsset cty_num date	
	gen xD=(1/xr_m)*euro_dollar
	gen dxD=ln(xD/l`lag'.xD)
	gen dxED=ln(xED/l`lag'.xED)
	*drop outliers: Venezuela, Sudan, Uzbekastan 
	drop if cty_code=="VE"|cty_code=="SD"|cty_code=="UZ"
	
	merge 1:1 cty_code date using temp/er_monthly_xE, keep(match) nogen
	
	keep cty_code cty_name date xE xD euro_dollar dxD dxE dxED
	
	gen test=xD/xE
	sum test euro_dollar
	
	save temp/er_monthly_xD,replace	



******************************************
*2. construct unit values in export data
******************************************	
use temp/exportdata, clear

	tsset fpd date
	gen lagvalue=l`lag'.value	
	gen duv=ln(uv1/l`lag'.uv1)
	replace duv=ln(uv2/l`lag'.uv2) if missing(duv) 
	
	gen dq=ln(q1/l`lag'.q1)
	replace dq=ln(q2/l`lag'.q2) if missing(dq)
		
	gen pratio=uv1/l`lag'.uv1
	replace pratio=uv2/l`lag'.uv2 if missing(pratio)
		
	drop if missing(duv)
		
	drop if (pratio>3 | pratio<1/3)
	
				
*3. combine data
***********************
	merge m:1 vat cty_code using ../data/currency_vat, keep(match) nogen 
	merge m:1 vat cty_code using ../data/currency_vehicle, keep(match master) nogen	

	merge m:1 cty_code date using temp/er_monthly_xD, keep(match) nogen
	merge m:1 vat using ../data/memp, keep(match master) nogen	
	merge m:1 vat using ../data/mphi_xeu_currency, keep(match) nogen
	

*merge manuf indicators
	merge m:1 vat using ../data/manuf_firm, keep(match master) nogen
	keep if manuf_firm==1

*merge in Rauch index
	gen hs6=substr(cncode,1,6)
	merge m:1 hs6 using ../data/Rauch_hs6_concordance, keep(match master) nogen
	gen dif=con=="n"
	
merge m:1 vat cty_code using ../data/currency_vehicle, keep(match master) nogen	
	*gen cutoff10=share_vehicle_other>0.1
	*tab cutoff10
	*drop if cutoff10==1	

	drop if vehicle_other==1

*************************************
*4. construct variables
***********************************
gen share_other=1-share_euro
	
*mphi_xeu_prod is noneuro and nonUSD
	gen dxE_share_euro=dxE*share_euro
	gen dxE_share_other=dxE*share_other
	gen dxD_share_usd=dxD*share_usd
	gen dxD_share_other=dxD*share_other
		
	gen lnmemp=ln(memp)
	gen dxE_lnmemp=dxE*lnmemp
	gen dxD_lnmemp=dxD*lnmemp	

			
*dxr interactions
	gen dxE_phi=dxE*mphi_xeu
	gen dxD_phi=dxD*mphi_xeu
	
	gen hs4=substr(cncode,1,4)	
	egen cp=group(hs4 cty_code)
	egen cy=group(year month cty_code)
	egen cpy=group(hs4 cty_code date)
	
*convert duv from euro prices to destination prices
	gen duv_k=duv + dxE	
			
save temp/temp_erpt_m`lag', replace


}

	
****************************************************************
*REGRESSIONS	
*all countries
*exclude ms variables
*********************************************
*baseline
***********************************
*1. with cp, date, vat fe

forv lag=1/24 {
use temp/temp_erpt_m`lag', clear

*1.i - all products
	eststo: reghdfe duv_k dxE_share_euro dxE_share_other dxD_share_usd dxE_phi dxD_phi dxD_lnmemp dxE_lnmemp , cluster(cy) absorb(cp date vat)

}

esttab using "../results/dynamic/dynamic_cp.csv", se  b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	


*1.ii - diff
********
forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	keep if dif==1
	eststo: reghdfe duv_k dxE_share_euro dxE_share_other dxD_share_usd dxE_phi dxD_phi dxD_lnmemp dxE_lnmemp , cluster(cy) absorb(cp date vat)
}

esttab using "../results/dynamic/dynamic_cp_dif.csv", se  b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	

*1.ii. non diff
********
forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	drop if dif==1
	eststo: reghdfe duv_k dxE_share_euro dxE_share_other dxD_share_usd dxE_phi dxD_phi dxD_lnmemp dxE_lnmemp , cluster(cy) absorb(cp date vat)
}

esttab using "../results/dynamic/dynamic_cp_nondif.csv", se  b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	


************************************************************************
*ADDITIONAL RESULTS
*****************************************************************************
*baseline - alternative clustering - firm-country*1. with cp, date, vat fe

forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	egen fc=group(vat cty_code)

*1.i - all products
	eststo: reghdfe duv_k dxE_share_euro dxE_share_other dxD_share_usd dxE_phi dxD_phi dxD_lnmemp dxE_lnmemp , cluster(fc) absorb(cp date vat)

}

esttab using "../results/dynamic/dynamic_cp_altcluster_fc.csv", se  b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	

*/

**************************************************************************
*2. as baseline but dxE instead of dxE_euro (to get significant difference)
***************************************************************************

forv lag=1/24 {
use temp/temp_erpt_m`lag', clear

*2.1 - all products		
	eststo: reghdfe duv_k dxE dxE_share_other dxD_share_usd dxE_phi dxD_phi dxD_lnmemp dxE_lnmemp , cluster(cy) absorb(cp date vat)

}

esttab using "../results/dynamic/dynamic_cp_v2.csv", se  b(3) se(3) r2 ar2  plain nostar  compress replace
eststo clear	


*2.ii - diff products
forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	keep if dif==1
	eststo: reghdfe duv_k dxE dxE_share_other dxD_share_usd dxE_phi dxD_phi dxD_lnmemp dxE_lnmemp , cluster(cy) absorb(cp date vat)


}

esttab using "../results/dynamic/dynamic_cp_dif_v2.csv", se  b(3) se(3) r2 ar2  plain nostar  compress replace
eststo clear	

forv lag=1/24 {

*2.iii - nondif
use temp/temp_erpt_m`lag', clear
	drop if dif==1
	eststo: reghdfe duv_k dxE dxE_share_other dxD_share_usd dxE_phi dxD_phi dxD_lnmemp dxE_lnmemp , cluster(cy) absorb(cp date vat)


}

esttab using "../results/dynamic/dynamic_cp_nodif_v2.csv", se  b(3) se(3) r2 ar2  plain nostar  compress replace
eststo clear	



****************************************************
*v3 - as v1 with cpy and vat fe
*********************************************************
forv lag=1/24 {
use temp/temp_erpt_m`lag', clear

*3.1 - all products
	eststo: reghdfe duv_k dxE_share_euro dxE_share_other dxD_share_usd dxE_phi dxD_phi dxD_lnmemp dxE_lnmemp , cluster(cy) absorb(cpy vat)
}

esttab using "../results/dynamic/dynamic_cpy.csv", se  b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	


*3.ii - dif
forv lag=1/24 {

use temp/temp_erpt_m`lag', clear
	keep if dif==1
	eststo: reghdfe duv_k dxE_share_euro dxE_share_other dxD_share_usd dxE_phi dxD_phi dxD_lnmemp dxE_lnmemp , cluster(cy) absorb(cpy vat)

}

esttab using "../results/dynamic/dynamic_cpy_dif.csv", se  b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	


*3.iii - nondif
***************
forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	drop if dif==1
	eststo: reghdfe duv_k dxE_share_euro dxE_share_other dxD_share_usd dxE_phi dxD_phi dxD_lnmemp dxE_lnmemp , cluster(cy) absorb(cpy vat)
}

esttab using "../results/dynamic/dynamic_cpy_nondif.csv", se  b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	

